package cn.jimu98.config;

import cn.jimu98.entity.Category;
import cn.jimu98.service.CategoryService;
import cn.jimu98.service.PostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * @Classname ContextStartup
 * @Description TODO
 * @Date 2020/6/7 18:31
 * @Created by kaibo
 */
@Component
public class ContextStartup implements ApplicationRunner, ServletContextAware {
    @Autowired
    CategoryService categoryService;

    ServletContext servletContext;

    @Autowired
    PostService postService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Category> categories = categoryService.list(new QueryWrapper<Category>()
                .eq("status", 0)
        );
        for (Category category : categories) {
            System.out.println(category);
        }

        servletContext.setAttribute("categorys",categories);

        postService.initWeekRank();
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;
    }
}
